using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using SamasUtilities;

namespace SamasDBI.SQLServer
{
    public class SQLRunner
    {
        private static string sErrorFile = "SqlServerRunnerErrors.txt";
        private static string sConnectionString = "";

        /// <summary>
        /// Gets or sets the connection string.
        /// </summary>
        /// <value>The connection string.</value>
        public static string ConnectionString
        {
            get { return sConnectionString; }
            set { sConnectionString = value; }
        }

        /// <summary>
        /// Gets or sets the error file.
        /// </summary>
        /// <value>The error file.</value>
        public static string ErrorFile
        {
            get { return sErrorFile; }
            set { sErrorFile = value; }
        }

        /// <summary>
        /// Executes the given query.
        /// </summary>
        /// <param name="SQL">The SQL Statement.</param>
        /// <param name="Connection">An active opened connection.</param>
        /// <param name="RecordSet">The record set.</param>
        /// <returns></returns>
        static public bool ExecuteQuery(string SQL, SqlConnection Connection, ref SqlDataReader RecordSet)
        {
            SqlCommand cmdGet = new SqlCommand(SQL, Connection);

            try
            {
                RecordSet = cmdGet.ExecuteReader();
            }
            catch (DataException dEx)
            {
                Log.WriteLine(dEx.Message, sErrorFile, true);
                return false;
            }
            catch (SqlException mEx)
            {
                Log.WriteLine(mEx.Message, sErrorFile, true);
                return false;
            }
            catch (Exception ex)
            {
                Log.WriteLine(ex.Message, sErrorFile, true);
                return false;
            }

            return true;
        }

        /// <summary>
        /// Executes the non query Sql Statement.
        /// </summary>
        /// <param name="SQL">The SQL Statement.</param>
        /// <param name="Connection">An active opened connection.</param>
        /// <returns></returns>
        static public bool ExecuteNonQuery(string SQL, SqlConnection Connection)
        {
            SqlCommand cmdExecute = new SqlCommand(SQL, Connection);

            try
            {
                cmdExecute.ExecuteNonQuery();
            }
            catch (DataException dEx)
            {
                Log.WriteLine(dEx.Message, sErrorFile, true);
                return false;
            }
            catch (SqlException mEx)
            {
                Log.WriteLine(mEx.Message, sErrorFile, true);
                return false;
            }
            catch (Exception ex)
            {
                Log.WriteLine(ex.Message, sErrorFile, true);
                return false;
            }

            return true;
        }
    }//class
}
